package common

import (
	"time"
)

//获取指定时间月初和月末的月末时间[含]
func GetMonthBeginEnd(dateTime time.Time) (begin time.Time, end time.Time) {
	begin = time.Date(dateTime.Year(), dateTime.Month(), 1, 0, 0, 0, 0, dateTime.Location())
	endDay := begin.AddDate(0, +1, -1)
	end = time.Date(endDay.Year(), endDay.Month(), endDay.Day(), 23, 59, 59, 0, dateTime.Location())
	return begin, end
}


//时间转时间戳
func DateTimeToTimestamp(datetime string) int64 {
	loc, _ := time.LoadLocation("Asia/Shanghai") //设置时区
	tt, err := time.ParseInLocation("2006-01-02 15:04:05", datetime, loc)
	if err != nil {
		//log.Error("datetime to timestamp error: ", err)
		return 0
	}
	return tt.Unix()
}

//时间转时间戳
func DateToTimestamp(datetime string) int64 {
	loc, _ := time.LoadLocation("Asia/Shanghai") //设置时区
	tt, err := time.ParseInLocation("2006-01-02", datetime, loc)
	if err != nil {
		//log.Error("date to timestamp error: ", err)
		return 0
	}
	return tt.Unix()
}

//时间戳 to 时间
func TimestampToDate(timestamp int64) string {
	return time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
}

//当前时间
func NowDatetime() string {
	return time.Now().Format("2006-01-02 15:04:05")
}

// 当前时间戳
func NowTimestamp() int64 {
	return time.Now().Unix()
}

/**
获取本周周一的日期
*/
func GetFirstDateOfWeek() (weekMonday string) {
	now := time.Now()

	offset := int(time.Monday - now.Weekday())
	if offset > 0 {
		offset = -6
	}

	weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
	weekMonday = weekStartDate.Format("2006-01-02")
	return
}
